package _04_queue;

import java.util.Iterator;
import java.util.StringJoiner;

/**
 * @author: mornd
 * @dateTime: 2023/6/5 - 10:03
 */
public abstract class AbstractQueue<E> implements Queue<E> {
    @Override
    public abstract boolean offer(E e);

    @Override
    public abstract E poll();

    @Override
    public abstract E peek();

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    @Override
    public void clear() {
        while (poll() != null) ;
    }

    /**
     * 有些实现类不需要记录 size
     * @return
     */
    @Override
    public int size() {
        throw new UnsupportedOperationException();
    }

    @Override
    public abstract boolean isFull();

    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner(", ", "[", "]");
        Iterator<E> it = iterator();
        while (it.hasNext()) {
            joiner.add(String.valueOf(it.next()));
        }
        return joiner.toString();
    }
}
